import java.util.Arrays;

public class Solution881 {
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);
        int count=0,i,j;
        for (i=0,j=people.length-1;i<j;){
            if (people[i]+people[j]<=limit){
                i++;
            }
            j--;
            count++;
        }
        if (i==j){
            count++;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(new Solution881().numRescueBoats(new int[]{3,2,2,1},3));
    }
}
